<?php

function view_keluarga_jabatan_detail($kid = 0  ){
	$kid = (int) $kid;
	if($kid == 0)my_direct($_SERVER['HTTP_REFERER']);
	my_set_code_js(' 
		function confirmDelete(i){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){ 
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+i ;
			}
			return false;
		}
	');	
 
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),   
		'unit  kompetensi'=>array('style'=>'border-bottom:2px solid;width:85%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
 
		$query = "SELECT * FROM  keluarga_jabatan_kompetensi a
		INNER JOIN kompetensi_unit b ON a.kompetensi_unit_id = b.kompetensi_unit_id
		WHERE a.keluarga_jabatan_id ={$kid}";
  
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=keluarga_jabatan_detail&task=edit&kid='.$kid.'&id='.$ey['keluarga_jabatan_kompetensi_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		
		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['keluarga_jabatan_kompetensi_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),   
			'nama'=>  $ey['nama_kompetensi_unit'] ,    
			'operasi'=> position_text_align(  $edit_button .' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$kel_jabatan = my_get_data_by_id('keluarga_jabatan','keluarga_jabatan_id',$kid); 	 
	 
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "detail pegawai" , "cp"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;"><input type="button" value="Tambah data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=keluarga_jabatan_detail&task=edit&kid='.$kid.'&id=0\' });" class="main_button" />
	<input type="button" value=" CETAK " class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=keluarga_jabatan_detail&kid='.$kid.'&id='.$_GET['kid'].'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/> <input type="button" value=" KEMBALI" class="main_button" onclick="javascript:location.href=\'index.php?com=keluarga_jabatan\'"/></div></td></tr>'   ; 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($kel_jabatan['keluarga_jabatan_label']).'</span>'   , "<b>Keluarga jabatan</b>"    ); 
	$view .= form_footer( );
	$view .= '</td></tr></table><br/>';
	return  $view. table_builder($header , $datas ,  3 , false );
}

function remove_job_family($id){
	return my_query("DELETE FROM keluarga_jabatan_kompetensi WHERE keluarga_jabatan_kompetensi_id = {$id}");
}

function form_keluarga_jabatan_create( $kid = 0 , $id){  
	$scripts = array('components/system/jquery/combomulti/jquery.chainedSelects.js');
	my_set_file_js($scripts);

	my_set_code_js(' 
	$(function()
	{ 
		$(\'#kategori_kompetensi_id\').chainSelect(\'#kompetensi_unit_id\',\'combobox_unit_kompetensi.php\',
		{ 
			before:function (target) 
			{ 
				$("#skomloading").css("display","block");
				$(target).css("display","none");
			},
			after:function (target) 
			{ 
				$("#skomloading").css("display","none");
				$(target).css("display","inline");
			}
		});
	});

	' );
	my_set_code_css('
	#skomloading
		{  
			 
			background:#ff0000;
			color:#fff;
			font-size:14px;
			font-familly:Arial;
			padding:2px; 
			display:none;
			float:left;
		}
	');

	$view = form_header( "keluarga_jabatan" , "keluarga_jabatan"  );
 	$fields = my_get_data_by_id('keluarga_jabatan_kompetensi','keluarga_jabatan_kompetensi_id',$id);
	if($fields){
		$sub_kompetensi = my_get_data_by_id('kompetensi_unit','kompetensi_unit_id',$fields['kompetensi_unit_id']);
		$jenis_kompetensi_id=$sub_kompetensi['kategori_kompetensi_id'];
		$kj = my_get_data_by_id('keluarga_jabatan','keluarga_jabatan_id',$fields['keluarga_jabatan_id']); 	 
	}else{
		$jenis_kompetensi_id=0;
		$kj = my_get_data_by_id('keluarga_jabatan','keluarga_jabatan_id',  $kid ); 
	} 
	$view .= form_field_display( $kj['keluarga_jabatan_label']   , "Keluarga jabatan");
	 
	$jeniss = array();
	$query = "SELECT * FROM   kompetensi_jenis";
	$result = my_query($query);
	while($row =my_fetch_array($result)){
		$jeniss[$row['kompetensi_jenis_id']] = $row['nama_kompetensi_jenis'];
	}
	$jenis = array(
		'name'	=>'kategori_kompetensi_id',
		'value'	=>(isset($_POST['kompetensi_jenis_id']) ? $_POST['kompetensi_jenis_id'] : $jenis_kompetensi_id ),
		'id'	=>'kategori_kompetensi_id'
	);
	$form_field_jenis = form_dropdown($jenis , $jeniss);
	$view .= form_field_display( $form_field_jenis   , "Jenis Kompetensi");
 
	 
	$kota = array();
	if($fields['kompetensi_unit_id']){
		$kota_query = "SELECT * FROM kompetensi_unit a
		INNER JOIN kompetensi_kelompok b ON a.kompetensi_kelompok_id = b.kompetensi_kelompok_id 
		WHERE b.kompetensi_jenis_id = {$jenis_kompetensi_id} ";
		$kota_result = my_query($kota_query);
		while( $ro = my_fetch_array($kota_result) ){
			$kota[$ro['kompetensi_unit_id']] = $ro['nama_kompetensi_unit'];
		}
	}
	else
	$kota[0] =  '- - - - Pilih - - - -' ;
	 
	$type_data  = array(
		'value' => ( $fields['kompetensi_unit_id'] ? $fields['kompetensi_unit_id'] : 0),
		'name' =>  'kompetensi_unit_id' ,
		'id' => 'kompetensi_unit_id' ,
	); 
	$unit_field =  form_dropdown($type_data , $kota);
 
 
	$gp = '<span id="skomloading" style="float:left;">Loading ...</span>
	 ';
	$view .= form_field_display( $gp ." ". $unit_field, 
	"Unit kompetensi"   );
	 
	
		$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 

function form_jabatan_keluarga_kompetensi_submit(  $kid=0 , $id=0){
	$datas = array(
		'kompetensi_unit_id'=>my_type_data_int($_POST['kompetensi_unit_id']),  
		'keluarga_jabatan_id'=>my_type_data_int($kid),
	);
	
	if( $id > 0 ){
		return my_update_record('keluarga_jabatan_kompetensi','keluarga_jabatan_kompetensi_id',$id,$datas);
	}
	
	return my_insert_record(  'keluarga_jabatan_kompetensi' , $datas );
}